<?php
/**
 * Created by PhpStorm.
 * User: qiaoli
 * Date: 15/11/5
 * Time: 下午3:10
 */
class Praisem extends CI_Model
{
    function __construct()
    {
        $this->load->model('usereventlogm');
    }

    /**
     * @param $uid
     * @param $event_id
     * @return array(bool, array)
     */
    public function insert_praise_log($uid,$event_id)
    {
        if (!$event_id) {
            return array(false, array());
        }
        $data = $this->get_praise_detail_lst_by_event_id($event_id);
        foreach ($data as $itm) {
            if ($itm["user_id"] == $uid) {
                // 已经点过赞
                return array(false, $data);
            }
        }

        $cg_data = array();
        $cg_data['user_id'] = strval($uid);
        $cg_data['event_id'] = strval($event_id);
        $cg_data['addtime'] = time();

        $this->send_dongtai_dianzan($uid, $event_id);
        $this->db->insert("ml_event_praise_log",$cg_data);
        $data = $this->get_praise_detail_lst_by_event_id($event_id);
        return array(true, $data);
    }

    public function send_dongtai_dianzan($user_id, $event_id) {
        if (!$event_id) {
            return false;
        }
        $event_info = $this->usereventlogm->get_user_event_detail($event_id);
        if (empty($event_info)) {
            return false;
        }

        $user_info = $this->userm->getUserInfoById($user_id);

        $title = $user_info["username"] . "赞了你的动态";
        $this->load->model('messagem');
        $this->messagem->snd_dongtai($user_id, $event_info["uid1"], $event_id, $title, $event_info["message"]);
        return true;
    }

    public function cancel_praise_log($uid,$event_id)
    {
        if($event_id)  // 不能为空
        {
            $result = $this->db->where(array("user_id"=>$uid, "event_id"=>$event_id))->delete("ml_event_praise_log");     //只删除一条
            return $result;
        }
    }

    public function is_praise_log($uid, $event_id)
    {
        $conbition = array("user_id"=>$uid, "event_id"=>$event_id);
        return $result = $this->db->from("ml_event_praise_log")->where($conbition)->count_all_results();
    }

    public function praise_list($event_id)
    {
        $result = $this->db->from("ml_event_praise_log")->where(array("event_id"=>$event_id))->order_by("addtime","desc")->get()->result_array();
        $uids = array();
        foreach($result as $oneRecord)
        {
            $user_id = $oneRecord['user_id'];
            $uids[] = $user_id;
        }
        return $uids;
    }

    public function get_praise_detail_lst_by_event_id($event_id) {
        $records = $this->db->from("ml_event_praise_log")->where(array("event_id"=>$event_id))->order_by("addtime","desc")->get()->result_array();
        $result = array();
        foreach($records as $oneRecord)
        {
            $one_rec = array();
            $user_id = $oneRecord['user_id'];
            $one_rec['user_id'] = $user_id;

            if(!$user_id) {
                continue;
            }

            $user_info = $this->userm->getUserInfoById($user_id);
            $username = $user_info['username'];
            $one_rec['username'] = $username;

            array_push($result, $one_rec);
        }
        return $result;
    }

    public function get_praise_lst_by_event_ids($event_ids) {
        if(!$event_ids)
        {
            return array();
        }
        $query = $this->db->from("ml_event_praise_log")->where_in("event_id", $event_ids)->order_by("addtime","desc")->get();

        if (!$query) {
            return array();
        }
        $records = $query->result_array();

        $result = array();
        foreach($records as $rec) {
            $one_rec = array();
            $user_id = $rec['user_id'];
            $one_rec['user_id'] = $user_id;
            if(!$user_id) {
                continue;
            }

            $user_info = $this->userm->getUserInfoById($user_id);
            $username = $user_info['username'];
            $one_rec['username'] = $username;
            $one_rec['event_id'] = $rec['event_id'];
            $tmp = isset($result[$one_rec["event_id"]]) ? $result[$one_rec["event_id"]] : array();

            array_push($tmp, $one_rec);

            $result[$rec["event_id"]] = $tmp;
        }

        return $result;
    }
}